//Vuex状态管理实现聊天信息存储

import { createStore } from 'vuex';

export default createStore({
    state: {
        //房间号
        roomNumber:"default",
        //聊天信息
        messages: [
            //id1-9为测试数据
            { text: '你好，请问我能为您做些什么?', receiver: true, request:'default'},
            // { text: 'Hi', receiver: false, request:'default' },
            // { text: 'How are you?', receiver: true, request:'default' },
            // { text: "I'm good, thanks!", receiver: false, request:'default' },
            // { text: "How can I help you?", receiver: true, request:'wifi' },
            // { text: "I want to book a room.", receiver: false, request:'default' },
            // { text: "I see, What kind of room do you need? ", receiver: true, request:'default' },
            // { text: "Can you give me some detailed information about the rooms?", receiver: false, request:'default' },
            // { text: "Sure! I have send relevant information to you. You can use your device to browse it and make better decisions.", receiver: true },
        ],
        //临时保存的传入的请求
        request_prior: "default",
        request_tmp: "default",
        //request请求类型
        request_roomNumber: '房间号',
        request_clean: '打扫服务',
        wifi:[
            {category:'wifi'},
            {category:'网络服务'},
        ],
        supply: [
            {category:'毛巾'},
            {category:'洗漱用品'},
            {category:'拖鞋'},
            {category:'水壶'},
            {category:'被子'},
        ],
        route:[
            {category:"聊天"},
            {category:"对话"},
            {category:"预定"},
            {category:"预约"},
            {category:"退房"},
            {category:"延迟退房"},
            {category:"酒店信息"},
        ],
    },
    //文件内调用的函数
    mutations: {
        addMessage(state, message) {
            state.messages.push(message);
        },
        setRequest(state, request){
            state.request_prior=state.request_tmp;
            state.request_tmp=request;
        },
        setRoomNumber(state, roomNumber){
            state.roomNumber=roomNumber;
        }
    },
    //可被外部调用的send类型的函数
    //外部调用方式：this.$store.dispatch('function', {keys:values});
    actions: {
        //向store内传送一条聊天消息
        sendMessage({ commit }, message) {
            commit('addMessage', message);
        },
        //向store内传递正在处理的临时请求request_tmp
        sendRequest({ commit }, request){
            commit('setRequest', request);
        },
        //向store内传递当前处理的房间号
        sendRoomNumber({ commit }, roomNumber){
            commit('setRoomNumber', roomNumber);
        }
    },
    //包装给外部的get类型的函数
    //外部调用方式：this.$store.getters.function;
    getters: {
        //获取聊天消息列表
        getMessages(state) {
            return state.messages;
        },
        //获取聊天消息条数
        getCountMessages(state) {
            return state.messages.length;
        },
        //通过遍历的方式获取临时请求的类型（wifi、supply之类的）
        getRequestType(state){
            let type="default";
            state.wifi.forEach(item=>{
            //     console.log("scan request of wifi if: "+item.category+", now request: "+state.request_tmp);
                if(item.category==state.request_tmp)
                    type='wifi';
            })
            state.supply.forEach(item=>{
            //     console.log("scan request of supply if: "+item.category+", now request: "+state.request_tmp);
                if(item.category==state.request_tmp)
                    type='supply';
            })
            state.route.forEach(item=>{
                //     console.log("scan request of supply if: "+item.category+", now request: "+state.request_tmp);
                    if(item.category==state.request_tmp)
                        type='route';
                })
            if(state.request_tmp=='房间号')
                type=state.request_roomNumber;
            if(state.request_tmp=='打扫服务')
                type=state.request_clean;
            return type;
        },
        //通过遍历的方式获取先前的请求，应对输入房间号的请求
        getPriorRequest(state){
            return state.request_prior;
        },
        getRoomNumber(state){
            return state.roomNumber;
        }
    }
});
